package main

import (
	"net/http"
	"fmt"
	"golang.org/x/text/transform"
	"io/ioutil"
	"io"
	"golang.org/x/text/encoding"
	"bufio"
	"golang.org/x/net/html/charset"
)

func main()  {
	resp,err:=http.Get("https://blog.csdn.net/erlib/article/details/24184883")
	if err!=nil{
		panic(err)
	}
	//fmt.Println(resp)
	//关闭http.get连接
	defer resp.Body.Close()
	//fmt.Println(resp.Body)
	//fmt.Println(resp.StatusCode)//状态码，200为正常，404找不到页面
	if resp.StatusCode!=http.StatusOK {
		fmt.Println("找不到页面：",resp.StatusCode)
	}

	//判断动态编码
	e:=determinEncoding(resp.Body)
	utf8obj:=transform.NewReader(resp.Body,e.NewDecoder())
	body,err:=ioutil.ReadAll(utf8obj)
	fmt.Println(string(body))
}

func determinEncoding(r io.Reader)encoding.Encoding  {
	bytes,err:=bufio.NewReader(r).Peek(1024)
	if err!=nil{
		panic(err)
	}
	//查找编码格式
	e,_,_:=charset.DetermineEncoding(bytes,"")
	//返回编码格式
	return e
}